package com.mr;

import java.security.MessageDigest;

public class MD5Encrypt {

  private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

  public static String byteArrayToString(byte[] b) {
    StringBuffer resultSb = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
      resultSb.append(byteToNumString(b[i]));
    }
    return resultSb.toString();
  }

  private static String byteToNumString(byte b) {
    int _b = b;
    if (_b < 0) {
      _b = 256 + _b;
    }
    return String.valueOf(_b);
  }

  public static String encode(String origin) {
    String resultString = null;
    try {
      resultString = new String(origin);
      MessageDigest md = MessageDigest.getInstance("MD5");
      //返回20位长度
      resultString = byteArrayToString(md.digest(resultString.getBytes())).substring(0,10);
    } catch (Exception ex) {

    }
    return resultString;
  }

  public static void main(String[] args) {
    System.out.println(encode("spirits"));
  }

}
